home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / communic / pcmail / main / spoolfil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  5.1 KB  |  187 lines

  1. /*++
  2. /* NAME
  3. /*    spoolfil,metafile 3
  4. /* SUMMARY
  5. /*    create message file and meta file
  6. /* PROJECT
  7. /*    pc-mail
  8. /* PACKAGE
  9. /*    general utilities
  10. /* SYNOPSIS
  11. /*    int spoolfil(path,meta_info,mesg_prefix,meta_prefix)
  12. /*    char *path;
  13. /*    char *meta_info;
  14. /*    int mesg_prefix;
  15. /*    int meta_prefix;
  16. /*
  17. /*    int metafile(path,string,...,(char *) 0)
  18. /*    char *path;
  19. /*    char *string;
  20. /* DESCRIPTION
  21. /*    spoolfil() creates a (message, meta) file pair in the spool
  22. /*    directory.
  23. /*
  24. /*    "path" should be null-terminated string with the name of an existing
  25. /*    file. The contents of that file are filtered to produce a
  26. /*    clean ASCII file.
  27. /*
  28. /*    "meta-info" is a string with additional information that is written to
  29. /*    the first line of a meta file (usually name of mail recipient, mail
  30. /*    originator or a comment describing the contents of the message file).
  31. /*    There should not be any embedded newline characters in this string.
  32. /*
  33. /*    "mesg_prefix," "meta_prefix" are prefixes that will be used for building
  34. /*    names for files in the spool directory.
  35. /*
  36. /*    If the message file given to spoolfil() looks like a mail message, a
  37. /*    line with Subject: information, extracted from the message, will be
  38. /*    appended to the meta file.
  39. /*
  40. /*    metafile() creates a file, writes the successive string arguments to
  41. /*    that file, each followed by a newline character. This function is
  42. /*    typically used to create a file with meta information (originator,
  43. /*    subject etc.).
  44. /* FUNCTIONS AND MACROS
  45. /*    ascopen(), ascget(), ascclose(), newseqno()
  46. /* DIAGNOSTICS
  47. /*    A nonzero return value indicates an error condition (see status.h)
  48. /* SEE ALSO
  49. /*    status(5)    return values
  50. /*    ascf(3)        ASCII filter
  51. /* BUGS
  52. /*    Wordprocessor control codes etc will be lost when spoolfil copies
  53. /*    a file.
  54. /* AUTHOR(S)
  55. /*    W.Z. Venema
  56. /*    Eindhoven University of Technology
  57. /*    Department of Mathematics and Computer Science
  58. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  59. /* CREATION DATE
  60. /*    Mon May 18 18:45:10 GMT+1:00 1987
  61. /* LAST MODIFICATION
  62. /*    90/01/22 13:02:40
  63. /* VERSION/RELEASE
  64. /*    2.1
  65. /*--*/
  66.  
  67. #include <stdio.h>
  68. #include <varargs.h>
  69.  
  70. #include "defs.h"
  71. #include "path.h"
  72. #include "ascf.h"
  73. #include "status.h"
  74. #include "ms_parse.h"
  75.  
  76. /* metafile - create a meta file */
  77.  
  78. /* VARARGS */
  79.  
  80. public int metafile(va_alist) 
  81. va_dcl
  82. {
  83.     char   *path;
  84.     char   *string;
  85.     FILE   *fp;
  86.     va_list ap;
  87.     int     ret = 0;
  88.  
  89.     va_start(ap);
  90.     path = va_arg(ap, char *);
  91.  
  92.     if ((fp = fopen(path, "w")) == 0) {
  93.     ret = E_WRITERR;
  94.     } else {
  95.     while ((string = va_arg(ap, char *)) != 0)
  96.         (void) fprintf(fp, "%s\n", string);
  97.     if (fflush(fp) || ferror(fp))
  98.         ret = E_WRITERR;
  99.     (void) fclose(fp);
  100.     }
  101.     va_end(ap);
  102.     return (ret);
  103. }
  104.  
  105. /* spoolfil - make arbitrary spool file */
  106.  
  107. public int spoolfil(fname, meta, msgpfx, auxpfx)
  108. char   *fname;
  109. char   *meta;
  110. int     msgpfx;
  111. int     auxpfx;
  112. {
  113.     register int newid = newseqno();    /* new message id */
  114.     register int stat;            /* some status */
  115.     char   *msgpath;            /* new message file */
  116.     char   *auxpath;            /* new meta file */
  117.     char    subj[BUFSIZ];        /* subject: info */
  118.  
  119.     msgpath = mesg_file(msgpfx, newid);        /* message file name */
  120.     auxpath = meta_file(auxpfx, newid);        /* meta file name */
  121.  
  122.     /* copy input file to spool file, try to get subject, check for errors */
  123.  
  124.     if (stat = copyfile(fname, msgpath, subj)) {/* read/write error */
  125.     (void) unlink(msgpath);            /* delete msg file */
  126.     return (stat);                /* notify caller */
  127.     }
  128.     /* create file for meta information, check for errors */
  129.  
  130.     if (stat = metafile(auxpath, meta, subj, (char *) 0)) {
  131.     (void) unlink(msgpath);            /* delete msg file */
  132.     (void) unlink(auxpath);            /* delete meta file */
  133.     return (stat);                /* notify caller */
  134.     }
  135.     /* when nothing went wrong */
  136.  
  137.     (void) chmod(msgpath, 0444);        /* make message read-only */
  138.     (void) chmod(auxpath, 0444);        /* make metafile read-only */
  139.     return (0);                    /* own error handling */
  140. }
  141.  
  142. /* copyfile - copy a file, filter it, optionally extract subject: info */
  143.  
  144. public int copyfile(from, to, subj)
  145. char   *from;
  146. char   *to;
  147. char   *subj;                /* may be a null pointer */
  148. {
  149.     register FILE *in,
  150.            *out;            /* file pointers */
  151.     int     ret = 0;            /* error status */
  152.  
  153.     if ((in = ascopen(from, "r")) == 0) {    /* cannot read ?? */
  154.     return (E_READERR);
  155.     } else if ((out = fopen(to, "w")) == 0) {    /* cannot write ?? */
  156.     (void) ascclose(in);
  157.     return (E_WRITERR);
  158.     } else {
  159.     char    buf[BUFSIZ];
  160.     register int context = MS_UUCP;
  161.  
  162.     if (subj)
  163.         *subj = 0;
  164.  
  165.     while (ascgets(buf, sizeof(buf), in)) {
  166.         (void) fputs(buf, out);
  167.         (void) putc('\n', out);
  168.  
  169.         /* extract the subject "on the fly" */
  170.  
  171.         if (subj != 0 && *subj == 0
  172.         && (context = ms_parse(context, buf)) == MS_HEADER)
  173.         (void) hscanf(buf, "Subject:", " %[^\n]", subj);
  174.     }
  175.  
  176.     /* perform the usual error checking */
  177.  
  178.     if (ferror(in))
  179.         ret = E_READERR;
  180.     else if (fflush(out) || ferror(out))
  181.         ret = E_WRITERR;
  182.     (void) ascclose(in);
  183.     (void) fclose(out);
  184.     return (ret);
  185.     }
  186. }
  187.